package com.whfc.fse.service.impl;

import com.whfc.common.enums.AppWarnModuleType;
import com.whfc.common.enums.CraneDeviceStatus;
import com.whfc.common.enums.LiftDeviceStatus;
import com.whfc.common.util.DateUtil;
import com.whfc.entity.dto.msg.AppMsgToUserDTO;
import com.whfc.entity.param.msg.AppMsgAddParam;
import com.whfc.fse.dao.FseWarnMapper;
import com.whfc.fse.dao.FseWarnRuleChannelMapper;
import com.whfc.fse.dao.FseWarnRuleMapper;
import com.whfc.fse.dao.FseWarnRuleUserMapper;
import com.whfc.fse.dto.FseWarnCheckDTO;
import com.whfc.fse.entity.FseWarn;
import com.whfc.fse.enums.FseType;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.service.FseWarnRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2020-05-28
 */
@Service
public class FseWarnRuleServiceImpl implements FseWarnRuleService {
    @Autowired
    private FseWarnRuleMapper fseWarnRuleMapper;
    @Autowired
    private FseWarnRuleChannelMapper fseWarnRuleChannelMapper;
    @Autowired
    private FseWarnRuleUserMapper fseWarnRuleUserMapper;
    @Autowired
    private FseWarnMapper fseWarnMapper;

    private static final String CONTENT = "%s在%s,%s,请您及时处理";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public AppMsgAddParam checkWarnRule(FseWarnCheckDTO warnCheckDTO) {
        logger.info("检测触发特种设备报警服务,param:{}", warnCheckDTO.toString());

        Integer type = warnCheckDTO.getType();
        Integer fseId = warnCheckDTO.getFseId();
        Date time = warnCheckDTO.getTime();
        Integer deptId = warnCheckDTO.getDeptId();
        String code = warnCheckDTO.getCode();

        // 升降机报警
        Integer ruleType = null;
        if (FseType.LIFT.getValue().equals(type)) {
            Integer liftState = warnCheckDTO.getLiftState();
            LiftDeviceStatus deviceStatus = LiftDeviceStatus.parseByValue(liftState);
            if (deviceStatus == null) {
                return null;
            }
            ruleType = deviceStatus.getRuleType();
        }
        // 塔吊报警
        else if (FseType.CRANE.getValue().equals(type)) {
            Integer craneState = warnCheckDTO.getCraneState();
            CraneDeviceStatus deviceStatus = CraneDeviceStatus.parseByValue(craneState);
            if (deviceStatus == null) {
                return null;
            }
            ruleType = deviceStatus.getRuleType();
        }

        // 查找报警规则
        Integer ruleId = fseWarnRuleMapper.selectByDeptIdAndRuleType(deptId, ruleType);
        if (ruleId == null) {
            return null;
        }
        String triggerValue = this.getTriggerValue(ruleType, warnCheckDTO);

        // 插入报警
        FseWarn fseWarn = new FseWarn();
        fseWarn.setDeptId(deptId);
        fseWarn.setRuleType(ruleType);
        fseWarn.setTriggerObjectId(String.valueOf(fseId));
        fseWarn.setTriggerTime(time);
        fseWarn.setWarnRuleId(ruleId);
        fseWarn.setTriggerValue(triggerValue);
        fseWarnMapper.insertSelective(fseWarn);
        Integer fseWarnId = fseWarn.getId();

        // 查找报警通知方式
        List<Integer> channelList = fseWarnRuleChannelMapper.selectByRuleId(ruleId);
        if (channelList.isEmpty()) {
            return null;
        }
        // 查找报警通知人
        List<AppMsgToUserDTO> userList = fseWarnRuleUserMapper.selectByRuleId(ruleId);
        if (userList.isEmpty()) {
            return null;
        }

        FseWarnRuleType fseWarnRuleType = FseWarnRuleType.parseByValue(ruleType);
        if (fseWarnRuleType == null) {
            return null;
        }
        String ruleStr = fseWarnRuleType.detail();
        String title = fseWarnRuleType.tittle();
        String timeStr = DateUtil.formatDate(time, "yyyy-MM-dd HH:mm:ss");
        String content = String.format(CONTENT, FseType.parseByValue(type).getDesc() + code, timeStr, ruleStr);

        // 返回插入消息的数据
        AppMsgAddParam param = new AppMsgAddParam();
        param.setChannelList(channelList);
        param.setContent(content);
        param.setDeptId(deptId);
        param.setModuleType(AppWarnModuleType.FSE.value());
        param.setObjectId(String.valueOf(fseWarnId));
        param.setTime(time);
        param.setTitle(title);
        param.setUserList(userList);
        return param;
    }


    /**
     * 获取触发值
     *
     * @param ruleType
     * @param warnCheckDTO
     * @return
     */
    private String getTriggerValue(Integer ruleType, FseWarnCheckDTO warnCheckDTO) {
        String triggerValue = "";
        if (FseWarnRuleType.CRANE_WEIGHT.value().equals(ruleType)) {
            triggerValue = String.valueOf(warnCheckDTO.getWeight());
        }
        if (FseWarnRuleType.CRANE_FORCE_MOMENT.value().equals(ruleType)) {
            triggerValue = String.valueOf(warnCheckDTO.getMomentPercent());
        }
        if (FseWarnRuleType.CRANE_RANGE.value().equals(ruleType) ) {
            triggerValue = String.valueOf(warnCheckDTO.getRange());
        }
        if (FseWarnRuleType.CRANE_HEIGHT_RANGE.value().equals(ruleType)) {
            triggerValue = String.valueOf(warnCheckDTO.getHeight());

        }
        if (FseWarnRuleType.CRANE_ROTARY_LIMIT.value().equals(ruleType) ) {
            triggerValue = String.valueOf(warnCheckDTO.getTurnAngle());
        }
        if (FseWarnRuleType.CRANE_WIND_SPEED.value().equals(ruleType)) {
            triggerValue = String.valueOf(warnCheckDTO.getWindSpeed());
        }
        if (FseWarnRuleType.LIFT_SPEED.value().equals(ruleType)) {
            triggerValue = String.valueOf(warnCheckDTO.getSpeed());
        }
        if (FseWarnRuleType.LIFT_WEIGH.value().equals(ruleType)) {
            triggerValue = String.valueOf(warnCheckDTO.getLiftWeight());
        }
        return triggerValue;

    }

}
